//
//  IMFowardContactController.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/7/30.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "IMFowardContactController.h"
#import "GroupMenberCell.h"
#import "SettingNormalCell.h"
#import "MicroDef.h"
#import "DepartDetailVC.h"

#import "GroupListVC.h"
#import "GroupMemberDetailVC.h"
#import "UIImageView+WebCache.h"
#import "CContactModel.h"
#import "CDepartmentModel.h"
#import "CDepartmentMgr.h"
#import "CXmppRequestMgr.h"
#import "CContactMgr.h"
#import "NSArray+PinYin.h"
#import "IMFowardDepartMentController.h"
#import "JKAlertDialog.h"
#import "CFolderMgr.h"
#import "CMessageModel.h"
#import "CMessageMgr.h"
#import "ChildHoodService.h"
#import "IMFowardGroupListVc.h"
#import "CRecentlyContactModel.h"
#import "CRecentlyContactMgr.h"
#import "CHChatRoomBase.h"
#import "UIBarButtonItem+category.h"
#import "EFengDiscussController.h"
#import "EFengMIneContactController.h"



#define KSECTIONHEIGHT 35.0f
#define KNORMALCELLHEIGHT 60.0f

@interface IMFowardContactController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *_tableView;
}
@property (nonatomic, strong) NSMutableArray *contactList;
@end

@implementation IMFowardContactController

- (NSMutableArray *)contactList
{
    if (_contactList == nil) {
        _contactList = [NSMutableArray array];
    }
    return _contactList;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
//UISearchDisplayDelegate
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"选择联系人"];
   
    
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"取消" target:self sel:@selector(cancel) titleFont:Font18 titleColor:WhiteColor];
     self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(backForPop) titleFont:Font18 titleColor:WhiteColor];

    _tableView = [[UITableView alloc] init];
    _tableView.frame = self.view.frame;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.sectionIndexBackgroundColor = ClearColor;
    _tableView.sectionIndexTrackingBackgroundColor = ClearColor;
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    
//    NSArray *array = [[[CRecentlyContactMgr alloc] init] loadAllRecentlyContacts];
//    CContactMgr *conMgr = [[CContactMgr alloc] init];
//    for (CRecentlyContactModel *model in array) {
//        CContactModel *contact = [conMgr loadNotDelContactWithUserId:model.userId];
//        if (contact) {
//            [self.contactList addObject:contact];
//            
//        }
//    }

}

- (void)backForPop
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)cancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, HARDWARE_SCREEN_HEIGHT - 64);
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    
    return 1;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
        static NSString *NormalCellIdentifier = @"SettingNormalCell";
        SettingNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (normalCell == nil) {
            normalCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingNormalCell" owner:self options:nil] objectAtIndex:0];
        }
        if (indexPath.section == 0) {
            [normalCell.icon setImage:[UIImage imageNamed:[self iconName:indexPath]] forState:UIControlStateNormal];
            normalCell.rightArrow.hidden = NO;
            [normalCell.titleLabel setText:[self titleName:indexPath]];
            normalCell.icon.layer.cornerRadius = 5.0f/2;
            normalCell.icon.layer.masksToBounds = YES;
            return normalCell;
            
        } else {
            
            return nil;
        }
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"组织架构";
                    break;
                case 1:
                    title = @"群";
                    break;
                case 2:
                    title = @"讨论组";
                    break;
                case 3:
                    title = @"我的联系人";
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            title = @"孙思明";
        }
            break;
        default:
            break;
    }
    return title;
}

-(NSString *)iconName:(NSIndexPath *)indexPath
{
    NSString *iconName = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    iconName = @"icon-organization";
                    break;
                case 1:
                    iconName = @"icon-group1";
                    break;
                case 2:
                    iconName = @"icon-comment";
                    break;
                case 3:
                    iconName = @"icon-mineContact";
                    break;
                    
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return iconName;
}



#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    if(section == 0)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UIView *endLine = [[UIView alloc] init];
    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:endLine];
    
    UILabel *label = [[UILabel alloc] init];
        [label setText:@"常用联系人"];
        
    [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
    label.frame = CGRectMake(15, 1, 200, KSECTIONHEIGHT-1);
    [headerView addSubview:label];
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    if(section != 1)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        CGFloat height = 60.0f;
        
        if(indexPath.row == 0 && indexPath.section == 0)
        {
            height = 60.0f;
        }
        return height;
        
    } else {
        return 60.0f;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
            switch (indexPath.section) {
                case 0:
                {
                    switch (indexPath.row) {
                        case 0:
                        {
                            [self showDepartDetailVC];
                        }
                            break;
                        case 1:
                        {
                            [self showGroupListVC];
                        }
                            break;
                        case 2:
                        {
                            [self showDiscussVc];
                        }
                            break;
                        case 3:
                        {
                            [self showMineContactVC];
                        }
                            break;
                            
                        default:
                            break;
                    }
                }
                    break;
                default:
                    break;
            }
}

- (void)showDiscussVc
{
    
    EFengDiscussController *discuss = [[EFengDiscussController alloc] init];
    discuss.isFromFoward = YES;
    [self.navigationController pushViewController:discuss animated:YES];
}

- (void)showMineContactVC
{
    
    EFengMIneContactController *mineContact = [[EFengMIneContactController alloc] init];
    mineContact.isFromFoward = YES;
    [self.navigationController pushViewController:mineContact animated:YES];
}


//-(void)fowardWithMember:(CContactModel *)member withCurUser:(CContactModel *)currentUser
//{
//    
//    [self dismissViewControllerAnimated:NO completion:^{
//        CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
//        chat.member = member;
//        chat.group = nil;
//        
//        UINavigationController *rotNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
//        UIViewController *vc = rotNav.childViewControllers[1];
//        [rotNav popToViewController:vc animated:NO];
//        
//        [rotNav pushViewController:chat animated:YES];
//        if ([ChildHoodService defaultService].fileModel == nil && [ChildHoodService defaultService].imgLocalFilePath.length > 0) {
//            UIImage *image = [UIImage imageWithContentsOfFile:[ChildHoodService defaultService].imgLocalFilePath];
//            [[CXmppRequestMgr shareInstance] sendImageMessage:image index:0 curUser:currentUser member:member group:nil];
////            [self cancel];
//        } else {
//            [[CXmppRequestMgr shareInstance] sendFile:[ChildHoodService defaultService].fileModel curUser:currentUser member:member group:nil];
////            [self cancel];
//        }
// 
//    }];
//}


-(void)showDepartDetailVC
{
    NSArray *deps = [[[CDepartmentMgr alloc] init] loadDepartments:@"0"];
    CDepartmentModel *dep = deps.lastObject;

    IMFowardDepartMentController *departDetail = [[IMFowardDepartMentController alloc] initWithDepartId:dep.DEPTID];
    //    CHSNavigationController *departDetailNav = [[CHSNavigationController alloc] initWithRootViewController:departDetail];
    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
    [self.navigationController pushViewController:departDetail animated:YES];
   
    
}

-(void)showGroupListVC
{
    IMFowardGroupListVc *groupList = [[IMFowardGroupListVc alloc] init];
    //    CHSNavigationController *departDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupList];
    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
    [self.navigationController pushViewController:groupList animated:YES];
    
}


@end
